clear all



use "$root\data\use.dta", replace



foreach y in pplanted planted planted_fsa failed_fsa prevented_fsa {
	replace corn_`y' = 0.0001 if missing(corn_`y') & !missing(soybeans_`y')
	replace soybeans_`y' = 0 if !missing(corn_`y') & corn_`y' > 0.01 & missing(soybeans_`y')
	replace corn_`y' = 0 if corn_`y' < 0.01 
}
gen total_planned = corn_pplanted + soybeans_pplanted
gen total_planted = corn_planted + soybeans_planted
gen lntotal_planned = ln(total_planned)
gen lntotal_planted = ln(total_planted)



foreach s in d0 d1 d2 d3 {
	gen `s'_b = (c_`s'_b * corn_planted + s_`s'_b * soybeans_planted)/total_planted
	gen `s'_p = (c_`s'_p * corn_planted + s_`s'_p * soybeans_planted)/total_planted	
}

foreach t in 2 5 8 11 14 17 20 23 26 29 32 35 38 {
	gen tbin`t'c_b = (c_tbin`t'c_b * corn_planted + s_tbin`t'c_b * soybeans_planted)/total_planted
	gen tbin`t'c_p = (c_tbin`t'c_p * corn_planted + s_tbin`t'c_p * soybeans_planted)/total_planted	
}

gen pdsi_w_b = (c_pdsi_w_b * corn_planted + s_pdsi_w_b * soybeans_planted)/total_planted
gen pdsi_w_p = (c_pdsi_w_p * corn_planted + s_pdsi_w_p * soybeans_planted)/total_planted

foreach y in lntotal_planned lntotal_planted  {
	reghdfe `y' d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_corn>10 & N_year_soybeans>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `y'

}


coefplot (lntotal_planned, offset(0.0) ciopts(color(black) )), bylabel(Total Planned Acres) || ///
	(lntotal_planted, offset(0.0) ciopts(color(black) )), bylabel(Total Planted Acres) ||, ///
	mcolor(black) mfcolor(white) vertical keep(d*_b d*_p) yline(0, lcolor(gray) lwidth(0.2)) /// 
	byopts(yrescale compact cols(1) graphregion(fcolor(white))) subtitle(, fcolor(white) bmargin(top) bcolor(white)) ///
	groups(d*_b = Pre-planting d*p=Planting) ytitle("% Change (x 100)") ylab(,labs(small)) xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3")
graph export "$root/results_replication/appendix_figures/a1_total_acres.png", as(png) name("Graph")  width(3200) height(2400) replace
